package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Dept;
import cn.itcast.service.system.DeptService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    //查询
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){
        PageInfo info = deptService.findAll(page, size,getLoginCompanyId());
        request.setAttribute("page",info);


        return "system/dept/dept-list";
    }

    //进入增加页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        List list = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList",list);
        return "system/dept/dept-add";
    }

    //增加
    @RequestMapping("/edit")
    public String edit(Dept dept){
        dept.setCompanyId(getLoginCompanyId());
        dept.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(dept.getId())){
            //增加
            deptService.save(dept);
        }else {
            //修改
            deptService.update(dept);
        }

        return "redirect:/system/dept/list.do";
    }

    //进入修改
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Dept dept = deptService.findBayId(id);
        request.setAttribute("dept",dept);
        List list = deptService.findAll(getLoginCompanyId());
        request.setAttribute("deptList",list);
        return "system/dept/dept-update";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        deptService.delete(id);
        return "redirect:/system/dept/list.do";
}

}
